export default {
	
	state: {
		//list 为空
		//list: [],
		
		list: [
			{
				checked: false,
				id: 11,
				title: '商品标题123',
				cover: "/static/images/demo/list/1.jpg",
				//选中商品属性
				attrs: [
					{
						title: '颜色',
						selected: 0,
						list: [
							{name: '火焰红'},
							{name: '炭黑'},
							{name: '冰川蓝'},
						]
					},
					{
						title: "容量",
						selected: 0,
						list: [
							{name: '64GB'},
							{name: '128GB'}
						]
					},
					{
						title: "套餐",
						selected: 0,
						list: [
							{name: '标配'},
							{name: '套餐一'},
							{name: '套餐二'}
						]
					}
				],
				pprice: 2300,
				num: 1,
				minnum:1,
				maxnum:10
			},
			{
				checked: false,
				id: 12,
				title: '商品标题456',
				cover: "/static/images/demo/list/2.jpg",
				//选中商品属性
				attrs: [
					{
						title: '颜色',
						selected: 0,
						list: [
							{name: '火焰红'},
							{name: '炭黑'},
							{name: '冰川蓝'},
						]
					},
					{
						title: "容量",
						selected: 0,
						list: [
							{name: '64GB'},
							{name: '128GB'}
						]
					},
					{
						title: "套餐",
						selected: 0,
						list: [
							{name: '标配'},
							{name: '套餐一'},
							{name: '套餐二'}
						]
					}
				],
				pprice: 3600,
				num: 1,
				minnum:1,
				maxnum:10
			}
		],
		
		//选中列表，存放选中的ID
		selectedList: [],
		
		//popup显示
		popupShow: 'none',
		
		//当前操作商品的索引
		popupIndex: -1,
	},
	
	//类似于计算属性
	getters: {
		//判断是否全选
		checkedAll: (state) => {
			return  state.list.length === state.selectedList.length
		},
		
		//合计
		totalPrice: (state) => {
			var total = 0
			state.list.forEach(v => {
				if(state.selectedList.indexOf(v.id) > -1) {
					total += v.pprice * v.num
				}
			})
			return total
		}, 
		
		//禁用全选
		disableSelectAll:(state) => {
			console.log(state.list.length === 0)
			return state.list.length === 0
		},
		
		//拿到当前修改属性的商品的数据
		popupData: (state) => {
			return state.popupIndex > -1 ? state.list[state.popupIndex] : {}
		}
	},
	
	//主要放一些同步方法
	mutations: {
		//初始化popIndex
		initPopupIndex(state, index) {
			state.popupIndex = index
		},
		
		//全选
		selectAll(state) {
			state.selectedList = state.list.map(v => {
				v.checked = true
				return v.id
			})
		},
		
		//取消全选
		unSelectAll(state) {
			state.list.forEach(v => {
				v.checked = false
			})
			state.selectedList = []
		},
		
		//选中 或者 取消选中一个商品
		selectItem (state, index) {
			let id = state.list[index].id
			let i = state.selectedList.indexOf(id)
			//之前是选中
			if (i > -1) {
				//取消选中
				state.list[index].checked = false
				//移除选中列表中的当前商品
				return state.selectedList.splice(i, 1)
			}
			// 之前没选中
			state.list[index].checked = true
			state.selectedList.push(id)
		},
		
		//删除选中
		delGoods(state) {
			state.list = state.list.filter(v => {
				return state.selectedList.indexOf(v.id) === -1
			})
		}, 
		
		//加入购物车
		addGoodsToCart(state, goods) {
			state.list.unshift(goods)
		}
	},
	
	//异步方法
	actions: {
		doSelectAll({commit, getters}) {
			getters.checkedAll ? commit('unSelectAll') : commit('selectAll')
		},
		
		doDelGoods({commit}) {
			uni.showModal({
				content:'是否删除选中',
				success: (res) => {
					if (res.confirm) {
						commit('delGoods')
						uni.showToast({
							title: '删除成功'
						});
					} 
				}
			})
		},
		
		//是否显示属性弹出层
		//显示
		doShowPopup({state, commit}, index) {
			commit('initPopupIndex', index)
			state.popupShow = 'show'
		},
		
		//隐藏
		doHidePopup({state}) {
			state.popupShow = 'hide',
			
			setTimeout(() => {
				state.popupShow = 'none'
				commit('initPopupIndex', -1)
			}, 200)
		},
	}
}